<?php
namespace app\admin\controller;
use think\Db;
/**
 * 算力
 * Class NoticeController
 * Author mww
 * @package app\admin\controller
 */
class PayController extends CommonController
{
    /**
     * 获得数据
     */
    public function getDatas()
    {
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        $_POST = $this->getDataByCampusid($_POST);
        $_POST['water_userid'] = session('loginSession')['user_id'];
        $pay = Db::table('ew_water')
            ->where($_POST)->where("water_type",'in','4,5,6')
            ->limit($rows * ($page - 1), $rows)->select();
        $total = Db::table('ew_water')
            ->where($_POST)->where("water_type",'in','4,5,6')
            ->count();
        $data['total'] = $total;
        $data['rows'] = $pay;
        return json_encode($data);
    }
    /**
     * 显示提现人民币页面
     */
    public function rmb_out()
    {
        $type = $_POST['type'];//1是人民币，2是以太币
        $id = session('loginSession')['user_id'];
        $admin = Db::table('ew_user')->where('user_id',$id)->find();
        $this->assign('admin',$admin);
        $this->assign('type',$type);
        return $this->fetch("pay/rmb_out");
    }
    /**
     * 人民币和以太币提现申请入库
     */
    public function ask_out()
    {
        $id = session('loginSession')['user_id'];
        $admin = Db::table('ew_user')->where('user_id',$id)->find();
        $type = $_POST['type'];
        $money = $_POST['money'];
        $data = array();
        $now = array();
        if($type == 1){
            $money = substr(sprintf("%.3f", $money),0,-1);
            $now['user_rmb'] = $admin['user_rmb'] - $money;
        }else{
            $money = substr(sprintf("%.6f", $money),0,-1);
            $now['user_ytb'] = $admin['user_ytb'] - $money;
        }
        $deal = $_POST['deal'];
        if(md5($deal) != $admin['user_dealpass']){
            $ci = 1;
            $jin = $admin['user_date'];
            $today = date('Y-m-d');
            if($jin == $today){
                $times = $admin['user_passtimes'];
                if($times >= 3){
                    return json_encode(['status'=>0,'msg'=>'交易密码已错误三次，请明天再试']);
                }
                $ci = $times+1;
            }
            Db::table('ew_user')->where('user_id',$id)->update(['user_passtimes'=>$ci,'user_date'=>$today]);
            $s = 3-$ci;
            if($s<=0){return json_encode(['status'=>10,'msg'=>"交易密码错误3次，请明天再试"]);}else{
                return json_encode(['status'=>10,'msg'=>"交易密码错误{$ci}次，还剩{$s}次"]);
            }

        }
        $account = trim($_POST['account'],' ');
        if($money <= 0){return json_encode(['status'=>0,'msg'=>'提现金额不能小于0']);}
        if($account == ''){return json_encode(['status'=>0,'msg'=>'账户不能为空']);}
        if($admin['user_rmb'] <= 0){return json_encode(['status'=>0,'msg'=>'请先充值！']);}
        if($type==1 && $admin['user_rmb']<$money){return json_encode(['status'=>0,'msg'=>'余额不足']);}
        if($type==2 && $admin['user_ytb']<$money){return json_encode(['status'=>0,'msg'=>'余额不足']);}
        $eth = Db::table('ew_eth')->where('eth_id',1)->find()['eth_price'];
        $data['water_userid'] = $id;
        $data['water_money'] = $money;
        $data['water_status'] = 2;
        $data['water_morb'] = $type;
        $data['water_type'] = 5;
        $data['water_out_status'] = 2;
        $data['water_date'] = date("Y-m-d");;
        $data['water_datetime'] = date("Y-m-d H:i:s");
        $data['water_base'] = $eth;
        $data['water_number'] = 'L' . date('YmdHis') . rand(10000,99999);
        $data['water_account'] = $account;
        //加了个账户名字=================================
        if($type==1){
            if($_POST['accountname'] == ''){return json_encode(['status'=>0,'msg'=>'账户名字不能为空']);}
            $data['water_accountname'] = $_POST['accountname'];
        }
        //==========end=================================
        Db::startTrans();
        try{
            Db::table('ew_user')->where('user_id',$id)->update($now);
            $y = Db::table('ew_user')->where('user_id',$id)->find();
            if($y['user_display'] == 1){
                $data['water_usermoney'] = $y['user_rmb'];
            }else{$data['water_usermoney'] = $y['user_ytb'];}
            Db::table('ew_water')->insert($data);
            // 提交事务
            Db::commit();
            if($type == 1){
                $this->duanxin($admin['user_phone'],9,$admin['user_nickname'],date('H:i'),$money);
            }else{
                $this->duanxin($admin['user_phone'],10,$admin['user_nickname'],date('H:i'),$money);
            }
            return json_encode(['status'=>200,'msg'=>'提交成功，请等待审核']);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return json_encode(['status'=>0,'msg'=>'操作失败，请联系管理员']);
        }

    }

    //===================这是管理员的操作分割线==============================================================

    /**
     * 获得数据
     */
    public function getadminDatas()
    {
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        $_POST = $this->getDataByCampusid($_POST);
        $min = '2000-02-02';
        $max = '2050-12-12';
        $where = array();
        if(!empty($_POST['start'])){
            $min = $_POST['start'];
            unset($_POST['start']);
        }
        if(!empty($_POST['end'])){
            $max = $_POST['end'];
            unset($_POST['end']);
        }
        $where['water_date'] = array('between',"$min,$max");
        $where['water_type'] = array('in','5,6');
        $pay = Db::table('ew_water')->alias('w')
            ->join('ew_user u','w.water_userid=u.user_id','LEFT')
            ->where($_POST)->where($where)->order('water_id desc')
            ->limit($rows * ($page - 1), $rows)->select();
        $total = Db::table('ew_water')->alias('w')
            ->join('ew_user u','w.water_userid=u.user_id','LEFT')
            ->where($_POST)->where($where)
            ->count();
        $data['total'] = $total;
        $data['rows'] = $pay;
        return json_encode($data);
    }
    /**
     * 显示审核提现页面
     */
    public function show_how()
    {
        $wid = $_POST['wid'];//1是人民币，2是以太币
        $data = Db::table('ew_water')->alias('w')
            ->join('ew_user u','w.water_userid=u.user_id','LEFT')
            ->where('water_id',$wid)->find();
        $this->assign('data',$data);
        return $this->fetch("pay/update");
    }
    /**
     * 显示审核提现页面
     */
    public function yesOrNo()
    {
        $wid = $_POST['wid'];
        $type = $_POST['type'];//1是同意，3是拒绝
        $remark = trim($_POST['remark'],' ');
        $userid = $_POST['userid'];
//        $morb = $_POST['morb'];
        $water = Db::table('ew_water')->where('water_id',$wid)->find();
        Db::startTrans();
        try{
            Db::table('ew_water')->where('water_id',$wid)
                ->update(['water_out_status'=>$type,'water_remark'=>$remark,'water_donetime'=>date('Y-m-d H:i:s')]);
            $u = Db::table('ew_user')->where('user_id',$userid)->find();
            if($type == 3){
                if($water['water_morb'] == 1){
                    $money = $water['water_money']+$u['user_rmb'];
                    Db::table('ew_user')->where('user_id',$userid)
                        ->update(['user_rmb'=>$money]);
                }else{
                    $money = $water['water_money']+$u['user_ytb'];
                    Db::table('ew_user')->where('user_id',$userid)
                        ->update(['user_ytb'=>$money]);
                }
                //如果拒绝就再增加一条流水
                $addtwols = Db::table('ew_water')->where('water_id',$wid)->find();
                $addtwols['water_usermoney'] = $money;
                $addtwols['water_type'] = 6;
                $addtwols['water_status'] = 1;
                unset($addtwols['water_id']);
                Db::table('ew_water')->insert($addtwols);
            }
            // 提交事务
            Db::commit();
            if($type == 1){
                $this->duanxin($u['user_phone'],11,$u['user_nickname'],date('H:i'));
            }else{
                $this->duanxin($u['user_phone'],12,$u['user_nickname'],date('H:i'));
            }
            return json_encode(['status'=>200,'msg'=>'操作成功']);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return json_encode(['status'=>0,'msg'=>'操作失败，请联系管理员']);
        }
    }
    /**
     * 显示审核提现页面
     */
    public function seewaterout()
    {
        $wid = $_POST['wid'];//1是人民币，2是以太币
        $data = Db::table('ew_water')->alias('w')
            ->join('ew_user u','w.water_userid=u.user_id','LEFT')
            ->where('water_id',$wid)->find();
        $this->assign('data',$data);
        return $this->fetch("pay/seewaterout");
    }

}